<!--
以此组件进行v-model的封装基础
-->
<template>
  <el-date-picker :readonly="readonlyValue" v-model="myValue" v-bind="$attrs" v-on="$listeners" :value-format="myFormat" :format="myFormat" :type="type"></el-date-picker>
</template>

<script>
export default {
  name: 'mc-date-picker',
  data () {
    return {
      myFormat:"",
      myValueFormat:"",
      cacheKey:"_mc_datepicker",
      readonlyValue:window.iframeUi?true:this.readonly,
      defaultValue:this.mcDefaultValue
    }
  },
  props:['value','mcDefaultValue','type','valueFormat','readonly','format'],
  watch:{

  },
  computed:{
    myValue:{
      get:function (){
        var _t =this;
        if(!this.value){
          var lastValue = this.getLastValue();
          if(lastValue!==null){
              _t.$nextTick(()=>{
                _t.syncVmodel(lastValue,false);
              })
          }else if(this.defaultValue){
            var dv = this.defaultValue;
            if(typeof this.defaultValue !=='string'){
              dv = this.dateFormat(this.defaultValue,this.findDefaultFmt());
            }
            var _t = this;
            _t.$nextTick(()=>{
                _t.syncVmodel(dv,false);
            })
            this.defaultValue=null;//默认值只用一次 后续就清理 不然用户手动删除都去不掉
            return dv;
          }
        }
        else if(this.value && typeof this.value=='object'){
          var dv = this.dateFormat(this.value,this.findDefaultFmt());
          _t.$nextTick(()=> {
            this.syncVmodel(dv, false);
          })
          return dv;
        }
        return this.value;
      },
      set:function (v){
        if(v && typeof v=='object'){
          v = this.dateFormat(v,this.findDefaultFmt());
        }
        this.syncVmodel(v,true);
      }
    }
  },
  beforeCreate(){

  },
  created(){
    this.myFormat = this.findDefaultFmt();
  },
  mounted () {

  },
  methods:{
      syncVmodel(v,cache){
          this.$emit("input",v);
          if(cache){
            this.rememberLastValue(v);
          }
      },
      //记录最后的值到localStorage
      rememberLastValue(v){
          if(typeof localStorage=='undefined'){
            return;
          }
          var uuid = this.$attrs['mc-uuid'];
          if(!uuid){
            return;
          }
          var cache=localStorage.getItem(this.cacheKey);
          if(cache){
            cache = JSON.parse(cache);
          }else{
            cache = {};
          }

          cache[uuid]=v;
          localStorage.setItem(this.cacheKey,JSON.stringify(cache));
      },
      getLastValue(){
          if(typeof localStorage=='undefined'){
            return null;
          }
          var uuid = this.$attrs['mc-uuid'];
          if(!uuid){
            return null;
          }
          var cache = localStorage.getItem(this.cacheKey);
          if(cache){
            return JSON.parse(cache)[uuid];
          }
          return null;
      },
      findDefaultFmt(){
          if(this.format){
            return this.format;
          }
          if(this.type){
            switch(this.type){
              case 'year': return 'yyyy';
              case 'month': return 'yyyy-MM';
              case 'date': return 'yyyy-MM-dd';
              case 'week': return 'yyyy第WW周';
            }
          }
          return 'yyyy-MM-dd HH:mm:ss';
      },
      isRange(){
        return this.type && this.type.indexOf("range")!=-1;
      },
      dateFormat( date,fmt) {
          if(!date){
            return null;
          }
          if(this.isRange()){
            return date;
          }
          fmt = fmt || 'yyyy-MM-dd HH:mm:ss'

          var str = fmt;
          var Week = ['日','一','二','三','四','五','六'];

          str=str.replace(/yyyy|YYYY/,date.getFullYear());
          str=str.replace(/yy|YY/,(date.getYear() % 100)>9?(date.getYear() % 100).toString():'0' + (date.getYear() % 100));
          var month = date.getMonth()+1;
          str=str.replace(/MM/,month>9?month.toString():'0' + month);
          str=str.replace(/M/g,month);

          str=str.replace(/w|W/g,Week[date.getDay()]);

          str=str.replace(/dd|DD/,date.getDate()>9?date.getDate().toString():'0' + date.getDate());
          str=str.replace(/d|D/g,date.getDate());

          str=str.replace(/hh|HH/,date.getHours()>9?date.getHours().toString():'0' + date.getHours());
          str=str.replace(/h|H/g,date.getHours());
          str=str.replace(/mm/,date.getMinutes()>9?date.getMinutes().toString():'0' + date.getMinutes());
          str=str.replace(/m/g,date.getMinutes());

          str=str.replace(/ss|SS/,date.getSeconds()>9?date.getSeconds().toString():'0' + date.getSeconds());
          str=str.replace(/s|S/g,date.getSeconds());

          return str;
      }
  }
}
</script>

<style lang="scss">

</style>
